package aco_test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import aco_serial.SerialMaster;
import aco.Master;

public class AcoTest {
	//file directory where the tsp files are located
	private static String dir = "files/";
	//list of tsp files to be tested
	private static String[] files = {"gr24.tsp", 
								"st70.tsp", 
								"kroA100.tsp", 
								"kroA200.tsp", 
								"lin318.tsp", 
								"pcb442.tsp", 
								"rat575.tsp", 
								"d657.tsp"}; 
	
	private static BufferedWriter bw;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			bw = new BufferedWriter(new FileWriter(new File("files/results.txt")));
			if(bw != null){
				bw.write("Parallel vs. Serial ACO Implementation Results\n");
				bw.write("Number of Processors: " + Runtime.getRuntime().availableProcessors() + "\n");
				//run a test for each tsp file in the list
				for(int i = 0; i < files.length; i++){
					//variables for the results
					//parallel
					double parallel_result_cost = 0;
					double parallel_result_time = 0;
					//serial
					double serial_result_cost = 0;
					double serial_result_time = 0;
					bw.write(i +". File: " + files[i] +"\n");
					
					//run the parallel implementation
					bw.write("Parallel results:\n");
					Master master = new Master(dir + files[i]);
					master.startAnts();
					
					parallel_result_cost = master.getResultCost();
					parallel_result_time = master.getResultTime();
					
					bw.write("\tCost: " + parallel_result_cost + "\n");
					bw.write("\tTime: " + parallel_result_time + "\n");
					bw.write("-------------------------------------------------\n");
					
					//run the serial implementation
					bw.write("Serial results:\n");
					
					SerialMaster serialMaster = new SerialMaster(dir + files[i]);
					serialMaster.startAnts();
					
					serial_result_cost = serialMaster.getResultCost();
					serial_result_time = serialMaster.getResultTime();
					
					bw.write("\tCost: " + serial_result_cost + "\n");
					bw.write("\tTime: " + serial_result_time + "\n");
					bw.write("-------------------------------------------------\n");
					
					//calculate the speedup
					double speedup = serial_result_time/parallel_result_time;
					bw.write("Speedup = " + speedup +"\n");
					bw.write("-------------------------------------------------\n");
					bw.write("*************************************************\n");
					bw.write("-------------------------------------------------\n");
				}
				bw.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
